package com._21cn._front.cms.api;

import com._21cn._front.utils.uc.UcTokenHelper;
import com._21cn.framework.http.HttpRequestInfo;
import com._21cn.framework.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping({"/api/uc"})
public class UcApiController {
    private static final Logger log = LoggerFactory.getLogger(UcApiController.class);

    @Autowired
    private UcTokenHelper ucTokenHelper;

    /**
     * 更新token
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping({"/token/update.do"})
    public void updateToken(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String token = reqInfo.getParameter("access_token","");
        if (!StringUtil.isEmpty(token)){
            ucTokenHelper.updateToken(token);
            log.error("===========> uc accessToken:"+token);
            response.setContentType("application/json; charset=utf8");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().write("{\"code\":\"0\", \"msg\":\"success\"}");
            return;
        }else {
            response.setContentType("application/json; charset=utf8");
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write("{\"code\":\"1\", \"msg\":\"no token\"}");
            return;
        }
    }
}
